import time, uvicorn
import multiprocessing
from proxypool.processors.server import app
from proxypool.processors.getter import Getter
from proxypool.processors.tester import Tester
from proxypool.settings import CYCLE_GETTER, CYCLE_TESTER, API_HOST, API_PORT, ENABLE_SERVER, IS_PROD, \
    ENABLE_GETTER, ENABLE_TESTER
from loguru import logger

tester_process, getter_process, server_process = None, None, None

class Scheduler():
    """
    调度者
    """

    def run_tester(self, cycle=CYCLE_TESTER):
        """
        运行调试模块
        """
        if not ENABLE_TESTER:
            logger.info('检测模块无效')
            return
        tester = Tester()
        loop = 0
        while True:
            logger.debug(f'tester 线程 {loop} 开始运行...')
            tester.run()
            loop += 1
            time.sleep(cycle)

    def run_getter(self, cycle=CYCLE_GETTER):
        """
        运行构建者
        """
        if not ENABLE_GETTER:
            logger.info('构建者无效')
            return
        getter = Getter()
        loop = 0
        while True:
            logger.debug(f'getter 线程 {loop} 开始执行...')
            getter.run()
            loop += 1
            time.sleep(cycle)

    def run_server(self):
        """
        启用服务
        """
        if not ENABLE_SERVER:
            logger.info('没有可用服务')
            return
        logger.info('代理服务器启动')
        uvicorn.run(app='proxypool.scheduler:app',host=API_HOST, port=API_PORT, reload=True, debug=True, workers=1)

    def run(self):
        global tester_process, getter_process, server_process
        try:
            logger.info('运行代理池...')
            if ENABLE_TESTER:
                tester_process = multiprocessing.Process(
                    target=self.run_tester)
                logger.info(f'运行检测模块, pid {tester_process.pid}...')
                tester_process.start()

            if ENABLE_GETTER:
                getter_process = multiprocessing.Process(
                    target=self.run_getter)
                logger.info(f'运行构建者, pid {getter_process.pid}...')
                getter_process.start()

            if ENABLE_SERVER:
                server_process = multiprocessing.Process(
                    target=self.run_server)
                logger.info(f'运行服务, pid {server_process.pid}...')
                server_process.start()

            tester_process and tester_process.join()
            getter_process and getter_process.join()
            server_process and server_process.join()
        except KeyboardInterrupt:
            logger.info('接收到错误的指令')
            tester_process and tester_process.terminate()
            getter_process and getter_process.terminate()
            server_process and server_process.terminate()
        finally:
            tester_process and tester_process.join()
            getter_process and getter_process.join()
            server_process and server_process.join()
            logger.info(
                f'tester 状态为： {"存活" if tester_process.is_alive() else "离线"}')
            logger.info(
                f'getter 状态为： {"存活" if getter_process.is_alive() else "离线"}')
            logger.info(
                f'server 状态为： {"存活" if server_process.is_alive() else "离线"}')
            logger.info('代理运行结束')